package updateDatabases;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Gets the Metacritic score from the web
 * @author Matthew Mizumoto
 *
 */

public class Metacritic {

  static Double meta;

  public static Double metaScore(String name) {
    ArrayList<String> sourceLines = new ArrayList<String>();
    String source = "";
    try {
      sourceLines = urlToArrayListMeta(setSearchPhraseMetacritic(name));
      for (String str : sourceLines) {
        source += (str);
      }
      meta = (setMeta(source, name)) / 10;
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    return meta;
  }

  public static void main(String[] args) {
    ArrayList<String> sourceLines = new ArrayList<String>();
    String searchPhrase = "halo 3";
    String source = "";
    try {
      sourceLines = urlToArrayListMeta(setSearchPhraseMetacritic(searchPhrase));
      for (String str : sourceLines) {
        source += (str);
      }
      meta = (setMeta(source, searchPhrase)) / 10;
      System.out.println(meta);
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      // e.printStackTrace();
    }
  }

  /**
   * 
   * @param url The URL to retrieve source code from.
   * @return sourceCode The source code from the URL.
   * @throws IOException
   */
  public static ArrayList<String> urlToArrayListMeta(String url) throws IOException {
    Scanner openFile;
    int counter = 0;
    ArrayList<String> sourceCode = new ArrayList<String>();
    URL passedURL = new URL(url);
    try {
      openFile = new Scanner(passedURL.openStream());
      while (openFile.hasNext()) {
        sourceCode.add(openFile.nextLine());
        counter++;
      }
    }
    catch (Exception e) {

    }
    return sourceCode;
  }

  /**
   * Sets search phrase to send to metacritic
   * 
   * @param searchPhrase The game to search
   * @return address The address to query metacritic
   */
  public static String setSearchPhraseMetacritic(String searchPhrase) {
    String address = "";
    try {
      searchPhrase = searchPhrase.trim();
      searchPhrase = searchPhrase.replaceAll(" ", "+");
      address = "http://www.metacritic.com/search/all/";
      address = address.concat(searchPhrase) + "/results";
    }
    catch (Exception e) {

    }

    return address;
  }

  /**
   * Gets the metacritic score from the metacritic source code.
   * 
   * @param source The source code to parse.
   */
  public static Double setMeta(String source, String searchPhrase) {
    source = source.replaceFirst("<.*?<div class=\"body\">", "<div class=\"body\">");
    source =
        source.replaceFirst("<.*<span class=\"data metascore score_favorable\">",
            "<span class=\"data metascore score_favorable\">");
    source = source.replaceFirst("<", "");
    source = source.replaceFirst("<.*", "");
    source = source.replaceFirst(".*>", "");
    Double metacritic = 0.0;
    try {
      metacritic = Double.parseDouble(source);
    }
    catch (Exception e) {

    }
    return metacritic;
  }
}
